package com.ids.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.ids.bean.EmployeeAttendanceStatistics;
import com.ids.service.EmployeeAttendanceScreenService;
import com.ids.util.Utlity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ExportToCSV extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private InputStream fileInputStream;
	int month;
	int year; 
//	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd  HH::mm::ss";
	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd_HH'.'mm";
	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	private Map<String, EmployeeAttendanceStatistics> employeeAttandence;
	@Override
		public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		String monthSelection = request.getParameter("month");
		String yearSelection = request.getParameter("year");
		String isMultilevelInReq = request.getParameter("isMultiplelevel");
		final String empCode = (String) session.get("empCode");
		boolean isMultiLevel = true;
		
		if(null!=monthSelection){
			month = Integer.parseInt(monthSelection);
			
		}else{
			month =new Date().getMonth() + 1;
			
		}
		if (null!=yearSelection){
			year = Integer.parseInt(yearSelection);
		}else{     
			
			year = new Date().getYear() + 1900;
		}
//		if(null!=isMultilevelInReq){
//			isMultiLevel = Boolean.parseBoolean(isMultilevelInReq);
//		}
		
		employeeAttandence = EmployeeAttendanceScreenService.getInstance()
		.getEmployeeAttandence(empCode,isMultiLevel,month,year);
		String data = getDataForCSV(employeeAttandence);
		File file = Utlity.getFile(data);
		 fileInputStream = new FileInputStream(file);
		return SUCCESS;
	}
	public String getDataForCSV(Map<String, EmployeeAttendanceStatistics> employeeAttandence){
		Collection<EmployeeAttendanceStatistics> empStatistics = employeeAttandence.values();
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("Attendance Statistics for  "+month+"/"+year).append("\n");
		strBuff.append("ECode,EName,P,SL,CL,PL,CO,LWP,OFF,%age");
		strBuff.append("\n");
		for (EmployeeAttendanceStatistics element : empStatistics) {
			strBuff.append(element.getDataForCSV()).append("\n");
		}
		return strBuff.toString();
	}
	public String getFileName() {
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	     String fileName = "Attendance_"+sdf.format(cal.getTime())+".cvs";
	      return fileName;
	}
	


}
